package com.yango.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yango.commonutil.utils.JwtUtils;
import com.yango.commonutil.utils.MD5;
import com.yango.member.entity.Member;
import com.yango.member.entity.vo.MemberVo;
import com.yango.member.entity.vo.RegisterVo;
import com.yango.member.mapper.MemberMapper;
import com.yango.member.service.MemberService;
import com.yango.serviceutil.exception.YangoException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author LiDingYin
 * @since 2021-07-30
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //用户登录-返回token字符串
    @Override
    public String login(Member member) {
        //获取用户名和密码
        String mobile = member.getMobile();
        String password = member.getPassword();
        //判断登录对象是否为空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new YangoException(20001, "用户名或密码不能为空!");
        }

        //条件构造器
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        //查询
        Member mobileMember = baseMapper.selectOne(wrapper);
        //判断手机号
        if (StringUtils.isEmpty(mobileMember)) {
            throw new YangoException(20001, "登录失败!没有这个手机号!");
        }
        //判断密码,密码使用MD5加密，所以需要使用MD5加密判断
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new YangoException(20001, "登录失败!密码错误!");
        }
        //校验是否被禁用
        Integer ifDisabled = mobileMember.getIsDisabled();
        if (ifDisabled == 1) {
            throw new YangoException(20001, "用户已被禁用!请联系管理员!");
        }
        //登录成功，使用jwt工具类生成token字符串
        String token = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        //更新用户最后登录时间
        mobileMember.setLastLoginTime(new Date());
        //更新
        baseMapper.updateById(mobileMember);
        //返回token
        return token;
    }

    //用户注册
    @Override
    public boolean register(RegisterVo registerVo) {
        //获取注册信息
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        //判断是否为空
        if (StringUtils.isEmpty(nickname) ||
                StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code)) {
            throw new YangoException(20001, "必填项不能为空!");
        }

        //从redis获取发送的验证码
        String mobileCode = redisTemplate.opsForValue().get(mobile);
        if (StringUtils.isEmpty(mobileCode)) {
            throw new YangoException(20001, "验证码已失效!");
        }
        //查询数据库中是否已存在相同的手机号
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count.intValue() > 0) {
            throw new YangoException(20001, "该手机号已被注册!请使用其他手机号注册!");
        }
        //添加注册信息到数据库
        Member member = new Member();
        member.setMobile(mobile);//设置手机号
        member.setNickname(nickname);//设置昵称
        member.setPassword(MD5.encrypt(password));//设置密码-使用MD5加密
        member.setIsDeleted(0);//设置用户状态
        //设置默认头像
        member.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        int insert = baseMapper.insert(member);
        return insert > 0;
    }

    //获取用户微信openid
    @Override
    public Member getOpenIdMember(String openid) {

        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openid);

        Member member = baseMapper.selectOne(queryWrapper);
        return member;
    }

    //统计网站每日注册人数
    @Override
    public Integer getRegisterCount(String day) {
        return baseMapper.getRegisterCount(day);
    }

    //统计网站每日登录人数
    @Override
    public Integer getLoginCount(String day) {
        return baseMapper.getLoginCount(day);
    }

    //分页条件查询会员列表
    @Override
    public void pageList(Page<Member> page, MemberVo memberVo) {
        //条件构造器
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        //根据创建时间降序排序
        wrapper.orderByDesc("create_time");
        //判断查询条件是否为空
        if (StringUtils.isEmpty(memberVo)) {
            baseMapper.selectPage(page, wrapper);
            return;
        }
        if (!StringUtils.isEmpty(memberVo.getMobile())) {
            wrapper.like("mobile", memberVo.getMobile());
        }
        if (!StringUtils.isEmpty(memberVo.getNickname())) {
            wrapper.like("nickname", memberVo.getNickname());
        }
        baseMapper.selectPage(page, wrapper);
    }

    //新增会员
    @Override
    public boolean saveMember(Member member) {
        //查询数据库中是否已存在相同的手机号
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", member.getMobile());
        Integer count = baseMapper.selectCount(wrapper);
        if (count.intValue() > 0) {
            throw new YangoException(20001, "该手机号已被注册!请使用其他手机号注册!");
        }
        //使用MD5加密密码
        member.setPassword(MD5.encrypt("111111"));
        if("男".equals(member.getSex())){
            member.setSex(2);
        }
        if("女".equals(member.getSex())){
            member.setSex(1);
        }
        Integer insert = baseMapper.insert(member);
        return insert != null && insert > 0;
    }

}
